package fr.insalyon.sims.broadcast.tests;

import java.io.IOException;
import java.net.InetAddress;
import fr.insalyon.sims.broadcast.network.*;

public class MCastStreamTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		int port = 2321;
		
		// initialize receiver
		MCastStreamReceiver receiver= new MCastStreamReceiver(InetAddress.getByName("224.0.0.1"),port);
		
		receiver.addDataListener(new DataListener() {
			public void dataReceived(DataEvent event) {
				String data = new String(event.getData(), event.getOffset(), event.getLength());
				System.out.println("[receiver] data received: " + data);
			}
		});
		receiver.addDataErrorListener( new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace(); }
			});
		receiver.connect();
		receiver.start();
		
		// initialize transmitter
		MCastStreamTransmitter transmitter = new MCastStreamTransmitter(InetAddress.getByName("224.0.0.1"), port);
		transmitter.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[transmitter] an exception occured!");
				event.getException().printStackTrace();
			}
		});
		
		transmitter.connect();
		transmitter.start();
		
		for (int i = 0; i < 10; i++) {
			String data = "hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !hello world !";
			transmitter.send(data.getBytes(), 0, data.length());
			
			System.out.println("[transmitter] hello world "+ i +" sent");
			
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
					//ignored
			}
		}
		
		
		receiver.stop();
		receiver.disconnect();
		transmitter.stop();
		transmitter.disconnect();

	}
	// ------------------------------------------------------- CONSTANTS -- //
	// ------------------------------------------------------ ATTRIBUTES -- //
	// ---------------------------------------------------- CONSTRUCTORS -- //
	// --------------------------------------------------------- METHODS -- //
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
